package com.hp.bon.sgw.wh.channel.especial.bizSlot;

import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.outside.BizSlot;
import com.hp.bon.sgw.wh.channel.outside.bizSlotOutQuery.utils.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class SoapChannelBizSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(SoapChannelBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        try {

            String reqSerial = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Write_Off/Payment_Serial_Nbr");
            String destinationId = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Write_Off/Acc_Nbr");
            String feeAmount = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Write_Off/Bank_code");
            String bankCode = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Write_Off/Acc_Nbr");
            String paymentMethod = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Write_Off/Payment_Method");

            String bankCodeSet = bizParam.getParamString(BizConsts.KEY_ESPECIAL_BANK_CODE_SET);
            com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
            com.alibaba.fastjson.JSONObject stdCcrRecharge = new com.alibaba.fastjson.JSONObject();
            com.alibaba.fastjson.JSONObject recharge = new com.alibaba.fastjson.JSONObject();

            String chargeSource = "";
            String[] bankCodeSets = bankCodeSet.split(",");
            for (String item : bankCodeSets) {
                if (String.valueOf(item.split("\\|")[0]).equals(bankCode)) {
                    chargeSource = String.valueOf(item.split("\\|")[1]);
                }
            }

            recharge.put("reqSerial", reqSerial);
            recharge.put("chargeSource", chargeSource);
            recharge.put("destinationId", destinationId);
            recharge.put("destinationIdType", "2");
            recharge.put("destinationAttr", "99");
            recharge.put("balanceItemTypeID", "0");
            recharge.put("unitTypeID", "0");
            recharge.put("feeAmount", feeAmount);
            recharge.put("BankCode", bankCode);
            recharge.put("PaymentMethod", paymentMethod);
            stdCcrRecharge.put("recharge", recharge);
            jsonObject.put("stdCcrRecharge", stdCcrRecharge);


            StringBuilder buildStringMes = new StringBuilder();
            buildStringMes.append(jsonObject.toJSONString());

            //发送请求
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
            String respStr = HttpClientUtils.postHttpStr(buildStringMes.toString(), webserviceUrl);
            if (respStr.equals("")) {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                return bizResult;
            }
            StringBuffer strBf = new StringBuffer();
            jsonToXmlStr(xmlMessage, JSONObject.parseObject(respStr), strBf, reqSerial);

            bizResult.setHttpBody(strBf.toString());
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info(e.toString());
        }
        return bizResult;
    }

    public static void jsonToXmlStr(XmlMessage xmlMessage, JSONObject json, StringBuffer buffer, String Payment_Serial_Nbr) {
        buffer.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">");
        buffer.append("<SOAP-ENV:Header/><SOAP-ENV:Body><ns3:SgwQueryAck xmlns:ns3=\"http://www.chinatelecom.cn/SGW/SOAP\">");
        buffer.append("<MID>" + xmlMessage.getMID() + "</MID>");
        buffer.append("<REPTAG>" + xmlMessage.getREPTAG() + "</REPTAG>");
        buffer.append("<RTT>" + xmlMessage.getRTT() + "</RTT>");
        buffer.append("<SIG>" + xmlMessage.getSIG() + "</SIG>");
        buffer.append("<RC>" + xmlMessage.getRC() + "</RC>");
        buffer.append("<DS>" + xmlMessage.getDS() + "</DS>");
        buffer.append("<DATA><![CDATA[<Service_Information>");
        buffer.append("<?xml version=\"1.0\"?>");
        buffer.append("<Result>" + json.get("errorCode") + "</Result>");
        buffer.append("<Write_Off_Information>");
        buffer.append("<Latn_Id></Latn_Id>");
        buffer.append("<Bill_Serial_Nbr>" + json.get("BillSerialNbr") + "</Bill_Serial_Nbr>");
        buffer.append("<Payment_Serial_Nbr>" + Payment_Serial_Nbr + "</Payment_Serial_Nbr>");
        buffer.append("</Write_Off_Information>");
        buffer.append("</Service_Information>]]></DATA></ns3:SgwQueryAck></SOAP-ENV:Body></SOAP-ENV:Envelope>");
    }
}
